package com.shwread.android.qysw.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shwread.android.qysw10000182.R;
import com.shwread.android.ui.customview.MyListView;
import com.shwread.android.ui.widget.refresh.PullToRefreshScrollView;
import com.shwread.android.ui.widget.refresh.PullToRefreshUtil;
import com.shwread.android.ui.widget.refresh.base.PullToRefreshBase;
import com.shwread.android.ui.widget.refresh.base.PullToRefreshBase.OnRefreshListener;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.shwread.http.base.TextHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


/**
 * @project qysw_e-commerce
 * @description 用一句话描述该页面的功能
 * @author zhangnannan
 * @Time 2015年3月20日 下午4:45:34
 */
public class GoodsOrderDeliverDetailActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener
{
	private Context                 context;
	private TextView                tvTitle;
	private Button                  butBack, butHome;
	private MyListView              lvList;
	private OrderDeliverAdapter     orderDeliverAdapter;
	private List                    resultInfo;
	private PullToRefreshScrollView psView;
	private LinearLayout            llayNoInfo;
	private LinearLayout            llayDetailInfo;
	
	private ImageView          ivDeliverIcon;
	private TextView                tvDeliverName;
	private TextView                tvDeliverNumber;
	
	private RelativeLayout          rlay_loading;
	private Handler                 handler = new Handler()
	                                        {
		                                        
		                                        @Override
		                                        public void handleMessage(Message msg) {
			                                        
		                                        }
		                                        
	                                        };
	
	@Override
	public Handler getUIHandler() {
		// TODO Auto-generated method stub
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.goods_order_deliver);
		context = this;
		findView();
		addListener();
		initData();
	}
	
	private void findView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		butBack = (Button) findViewById(R.id.common_header_back);
		butHome = (Button) findViewById(R.id.common_header_home);
		psView = (PullToRefreshScrollView) findViewById(R.id.goods_order_deliver_psview);
		lvList = (MyListView) findViewById(R.id.goods_order_deliver_list_lv);
		
		tvTitle.setText("物流信息");
		butBack.setVisibility(View.VISIBLE);
		butHome.setVisibility(View.VISIBLE);
		llayNoInfo = (LinearLayout) findViewById(R.id.deliver_no_info_llay);
		llayDetailInfo = (LinearLayout) findViewById(R.id.deliver_detail_info_llay);
		llayNoInfo.setVisibility(View.VISIBLE);
		llayDetailInfo.setVisibility(View.GONE);
		
		ivDeliverIcon = (ImageView) findViewById(R.id.goods_order_deliver_image_iv);
		tvDeliverName = (TextView) findViewById(R.id.goods_order_deliver_name_tv);
		tvDeliverNumber = (TextView) findViewById(R.id.goods_order_deliver_number_tv);
	}
	
	private void initData() {
		resultInfo = new ArrayList();
		orderDeliverAdapter = new OrderDeliverAdapter();
		lvList.setAdapter(orderDeliverAdapter);
		
		getData();
		ivDeliverIcon.setBackgroundResource(R.drawable.default_deliver_icon);
		
	}
	
	/**
	 * 调用借口请求数据
	 */
	private void getData() {
		
	}
	
	class GetDataAction2
	        extends
	        TextHttpResponseHandler
	{
		
		/*
		 * (non-Javadoc)
		 * @see com.shwread.http.base.TextHttpResponseHandler#onFailure(int,
		 * org.apache.http.Header[], java.lang.String, java.lang.Throwable)
		 */
		@Override
		public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
			// TODO Auto-generated method stub
			
		}
		
		/*
		 * (non-Javadoc)
		 * @see com.shwread.http.base.TextHttpResponseHandler#onSuccess(int,
		 * org.apache.http.Header[], java.lang.String)
		 */
		@Override
		public void onSuccess(int statusCode, Header[] headers, String responseString) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	class GetDataAction1
	        extends
	        ObjectHttpResponseHandler<Object>
	{
		
		/*
		 * (non-Javadoc)
		 * @see
		 * com.shwread.http.base.ObjectHttpResponseHandler#onFailure(int,
		 * org.apache.http.Header[], java.lang.String, java.lang.Throwable)
		 */
		@Override
		public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
			// TODO Auto-generated method stub
			
		}
		
		/*
		 * (non-Javadoc)
		 * @see
		 * com.shwread.http.base.ObjectHttpResponseHandler#onSuccess(int,
		 * org.apache.http.Header[], java.lang.Object, java.lang.String)
		 */
		@Override
		public void onSuccess(int statusCode, Header[] headers, Object object, String responseString) {
			// TODO Auto-generated method stub
			
		}
		
		/*
		 * (non-Javadoc)
		 * @see com.shwread.http.base.ObjectHttpResponseHandler#getType()
		 */
		@Override
		public Type getType() {
			// TODO Auto-generated method stub
			return null;
		}
	}
	
	/**
	 * 
	 */
	@SuppressWarnings ("unchecked")
	private void addListener() {
		butBack.setOnClickListener(this);
		butHome.setOnClickListener(this);
		
		PullToRefreshUtil.initLabel(context, psView);
		
		psView.setOnRefreshListener(new OnRefreshListener()
		{
			
			@Override
			public void onRefresh(PullToRefreshBase refreshView) {
				if (psView.isHeaderShown())
				{
					refrush();
				}
				else if (psView.isFooterShown())
				{
					loadingMore();
				}
			}
		});
	}
	
	/**
	 * 上拉加载更多
	 */
	protected void loadingMore() {
		handler.postDelayed(new Runnable()
		{
			
			@Override
			public void run() {
				psView.onRefreshComplete();
			}
		}, 5000);
	}
	
	/**
	 * 下拉刷新
	 */
	protected void refrush() {
		handler.postDelayed(new Runnable()
		{
			
			@Override
			public void run() {
				psView.onRefreshComplete();
			}
		}, 5000);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				finish();
				
				break;
			case R.id.common_header_home:
				//				startActivity(new Intent(GoodsOrderDeliverDetailActivity.this, MainActivity.class));
				BaseSimpleActivity.gotoMainActivity();
				//				Utils.showToast(context, "您点击了HOME");
				break;
			
			default:
				break;
		}
	}
	
	private class OrderDeliverAdapter
	        extends
	        BaseAdapter
	{
		
		/*
		 * (non-Javadoc)
		 * @see android.widget.Adapter#getCount()
		 */
		@Override
		public int getCount() {
			return 24;
			//			return (resultInfo ==null||resultInfo.size()==0)?0:resultInfo.size();
		}
		
		/*
		 * (non-Javadoc)
		 * @see android.widget.Adapter#getItem(int)
		 */
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			//			return "";
			return (resultInfo == null || resultInfo.size() == 0) ? "" : resultInfo.get(position);
		}
		
		/*
		 * (non-Javadoc)
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		/*
		 * (non-Javadoc)
		 * @see android.widget.Adapter#getView(int, android.view.View,
		 * android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder;
			if (convertView == null)
			{
				holder = new Holder();
				convertView = LayoutInflater.from(context).inflate(R.layout.goods_order_deliver_item, null);
				holder.ivImg = (ImageView) convertView.findViewById(R.id.goods_order_deliver_item_selected_iv);
				holder.tvDeliverText = (TextView) convertView.findViewById(R.id.goods_order_deliver_item_context_tv);
				holder.tvDeliverTime = (TextView) convertView.findViewById(R.id.goods_order_deliver_item_time_tv);
				convertView.setTag(holder);
			}
			else
			{
				holder = (Holder) convertView.getTag();
			}
			return convertView;
		}
		
	}
	
	private class Holder
	{
		private TextView  tvDeliverTime;
		private TextView  tvDeliverText;
		private ImageView ivImg;
	}
	
}
